home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_002 / microemacs / fileio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  121 lines

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here. A better message writing scheme should
  4.  * be used.
  5.  */
  6. #include        <stdio.h>
  7. #include        "ed.h"
  8.  
  9. FILE    *ffp;                           /* File pointer, all functions. */
  10.  
  11. /*
  12.  * Open a file for reading.
  13.  */
  14. ffropen(fn)
  15. char    *fn;
  16. {
  17.         if ((ffp=fopen(fn, "r")) == NULL)
  18.                 return (FIOFNF);
  19.         return (FIOSUC);
  20. }
  21.  
  22. /*
  23.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  24.  * (cannot create).
  25.  */
  26. ffwopen(fn)
  27. char    *fn;
  28. {
  29. #if     VMS
  30.         register int    fd;
  31.  
  32.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  33.         || (ffp=fdopen(fd, "w")) == NULL) {
  34. #else
  35.         if ((ffp=fopen(fn, "w")) == NULL) {
  36. #endif
  37.                 mlwrite("Cannot open file for writing");
  38.                 return (FIOERR);
  39.         }
  40.         return (FIOSUC);
  41. }
  42.  
  43. /*
  44.  * Close a file. Should look at the status in all systems.
  45.  */
  46. ffclose()
  47. {
  48. #if     V7
  49.         if (fclose(ffp) != FALSE) {
  50.                 mlwrite("Error closing file");
  51.                 return(FIOERR);
  52.         }
  53.         return(FIOSUC);
  54. #endif
  55.         fclose(ffp);
  56.         return (FIOSUC);
  57. }
  58.  
  59. /*
  60.  * Write a line to the already opened file. The "buf" points to the buffer,
  61.  * and the "nbuf" is its length, less the free newline. Return the status.
  62.  * Check only at the newline.
  63.  */
  64. ffputline(buf, nbuf)
  65. char    buf[];
  66. {
  67.         register int    i;
  68.  
  69.         for (i = 0; i < nbuf; ++i)
  70.                 fputc(buf[i]&0xFF, ffp);
  71.  
  72.         fputc('\n', ffp);
  73.  
  74.         if (ferror(ffp)) {
  75.                 mlwrite("Write I/O error");
  76.                 return (FIOERR);
  77.         }
  78.  
  79.         return (FIOSUC);
  80. }
  81.  
  82. /*
  83.  * Read a line from a file, and store the bytes in the supplied buffer. The
  84.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  85.  * at the end of the file that don't have a newline present. Check for I/O
  86.  * errors too. Return status.
  87.  */
  88. ffgetline(buf, nbuf)
  89. register char   buf[];
  90. {
  91.         register int    c;
  92.         register int    i;
  93.  
  94.         i = 0;
  95.  
  96.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  97.                 if (i >= nbuf-1) {
  98.                         mlwrite("File has long line");
  99.                         return (FIOERR);
  100.                 }
  101.                 buf[i++] = c;
  102.         }
  103.  
  104.         if (c == EOF) {
  105.                 if (ferror(ffp)) {
  106.                         mlwrite("File read error");
  107.                         return (FIOERR);
  108.                 }
  109.  
  110.                 if (i != 0) {
  111.                         mlwrite("File has funny line at EOF");
  112.                         return (FIOERR);
  113.                 }
  114.                 return (FIOEOF);
  115.         }
  116.  
  117.         buf[i] = 0;
  118.         return (FIOSUC);
  119. }
  120.  
  121.